<?php

namespace app\data\model;

use think\admin\Model;

/**
 * 商城商品主模型
 * Class ShopGoods
 * @package app\data\model
 */
class ShopGoods extends Model
{
    /**
     * 格式化创建时间
     * @param string $value
     * @return string
     */
    public function getCreateAtAttr(string $value): string
    {
        return format_datetime($value);
    }
    
    // 获得第三级分类商品
    public function CateShop($cateids,$pagenum,$index,$priceOrder){
        $data=[];
        // halt($index);
        if ($index == 0) {
            //综合排序
                 $select = ShopGoods::where('cateids','like','%,'.$cateids.',')->order('id','desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
            foreach ($select as $item){
                 $data[]=$this->list_tree($item); //调用下面的方法
            }
        }else if($index == 1){
            //销量筛选
            $select = ShopGoods::where('cateids','like','%,'.$cateids.',')->order('stock_sales','desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
            foreach ($select as $item){
                 $data[]=$this->list_tree($item); //调用下面的方法
            }
        }else if($index == 2){
            //价格筛选
            if($priceOrder==1){
                //低到高筛选
                $select = ShopGoods::where('cateids','like','%,'.$cateids.',')->order('price_selling','asc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
                foreach ($select as $item){
                     $data[]=$this->list_tree($item); //调用下面的方法
                }
            }else if($priceOrder==2){
                //低到高筛选
                $select = ShopGoods::where('cateids','like','%,'.$cateids.',')->order('price_selling','desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
                foreach ($select as $item){
                     $data[]=$this->list_tree($item); //调用下面的方法
                }
            }
            
        }
       
        return $data;
    }
    
    //获得第一级分类商品
    public function CateFidShop($cateids,$pagenum,$index,$priceOrder){
        $data=[];
        // halt($index);
        if ($index == 0) {
            //综合排序
                 $select = ShopGoods::where('cateids','like',','.$cateids.',%')->order('id','desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
            foreach ($select as $item){
                 $data[]=$this->list_tree($item); //调用下面的方法
            }
        }else if($index == 1){
            //销量筛选
            $select = ShopGoods::where('cateids','like',','.$cateids.',%')->order('stock_sales','desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
            foreach ($select as $item){
                 $data[]=$this->list_tree($item); //调用下面的方法
            }
        }else if($index == 2){
            //价格筛选
            if($priceOrder==1){
                //低到高筛选
                $select = ShopGoods::where('cateids','like',','.$cateids.',%')->order('price_selling','asc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
                foreach ($select as $item){
                     $data[]=$this->list_tree($item); //调用下面的方法
                }
            }else if($priceOrder==2){
                //低到高筛选
                $select = ShopGoods::where('cateids','like',','.$cateids.',%')->order('price_selling','desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
                foreach ($select as $item){
                     $data[]=$this->list_tree($item); //调用下面的方法
                }
            }
            
        }
        return $data;
        
        // $select = ShopGoods::where('cateids','like',','.$cateids.',%')->order('id','desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
        // foreach ($select as $item){
        //      $data[]=$this->list_tree($item); //调用下面的方法
        // }
    }
    
    public function SearchData($searchVal,$pagenum,$index,$priceOrder){
        $data=[];
        //低到高筛选
        $select = ShopGoods::where('name','like','%'.$searchVal.'%')->order('price_selling','asc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
        foreach ($select as $item){
             $data[]=$this->list_tree($item); //调用下面的方法
        }
        return $data;
    }
    
    // getDetail字符串转数组
    public function list_tree($data){
        $data['data_items'] = json_decode($data['data_items']);
        $data['data_specs'] = json_decode($data['data_specs']);
        $data['slider'] = explode('|', $data['slider']);
        $data['cateids'] = explode(',', $data['cateids']);
        return $data;
    }
}